/**
 * Copyright (C) 2021 - 2031 O-Cubes Co., Ltd.
 */

/****************************************************************
 *  @file    rtc_test.c
 *  @brief   RTC testcase source file
 *  @version v1.0
 *  @date    03. Apr. 2023
 ****************************************************************/

#include "printf.h"
#include "hal_api.h"
#include "irq.h"

#define TEST_RTC_ALRAM_SEC 2

/**
 *  datatime print function
 */
static void rtc_time_print(Datetime_t_Ptr time)
{
	printf("[RTC]: datetime, %4ld-%02ld-%02ld %02ld:%02ld:%02ld\r\n",
			time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
			time->tm_hour, time->tm_min, time->tm_sec);
}

/**
 *  rtc interrupt callback function
 */
static void rtc_irq_handler(void *ptr)
{
	printf("[RTC]: rtc_irq_handler\r\n");
	rtc_time_print((struct datetime *)ptr);
	Hal_Irq_Disable(RTC_IRQn);
}

/**
 *  rtc test function
 */
int32_t test_rtc(void)
{
	struct datetime time;

	printf("\r\n[RTC]: TEST START\r\n");

	Hal_Rtc_Start();

	Hal_Rtc_GetDatetime((Datetime_t_Ptr)&time);
	rtc_time_print(&time);

	Hal_Rtc_RegisterCb(rtc_irq_handler);
	Hal_Rtc_SetAlarm(TEST_RTC_ALRAM_SEC);

	__WFI();

	Hal_Cnt_DelayMs(1000);

	Hal_Rtc_GetDatetime((Datetime_t_Ptr)&time);
	rtc_time_print(&time);

	Hal_Rtc_Stop();

	printf("[RTC]: TEST END\r\n");
	return 0;
}

